﻿using System;
namespace Doc.Xroad.Eu.Messages {
    
    
    [global::Xtee.Core.Attributes.XmlTypeAttribute(Name="GetDocumentListResponse", Namespace="http://doc.x-road.eu/", Target=typeof(GetDocumentListResponse))]
    public class GetDocumentListResponseSerializer : global::Xtee.Core.Types.AbstractXteeSerializable {
        
        protected GetDocumentListResponse clazz;
        
        public GetDocumentListResponseSerializer(GetDocumentListResponse clazz) {
            this.clazz = clazz;
        }
        
        public override void WriteXmlAttributes(global::Xtee.Core.IXteeXmlWriter Writer) {
            base.WriteXmlAttributes(Writer);
        }
        
        public override void WriteXml(global::Xtee.Core.IXteeXmlWriter Writer) {
            base.WriteXml(Writer);
            Writer.WriteStartElement("AllDocuments");
            if (clazz.AllDocuments!=null) {
                for (int x = 0; ((clazz.AllDocuments != null) 
                            && (x < clazz.AllDocuments.Count)); x = (x + 1)) {
                    if (clazz.AllDocuments[x]!=null) {
                        Writer.WriteStartElement("item");
                        global::Xtee.Core.SerializationMetadata metaAllDocuments = this.GetMetaData(clazz.AllDocuments[x]);
                        if (ShouldWriteTypeAttribute(typeof(global::Doc.Xroad.Eu.Document), clazz.AllDocuments[x].GetType())) {
                            Writer.WriteTypeAttribute(metaAllDocuments.TypeQualifiedName.Namespace, metaAllDocuments.TypeQualifiedName.Name);
                        }
                        var allDocumentsSerializer = metaAllDocuments.NewSerializer(clazz.AllDocuments[x]);
                        allDocumentsSerializer.WriteXmlAttributes(Writer);
                        allDocumentsSerializer.WriteXml(Writer);
                        Writer.WriteEndElement();
                    }
                }
            }
            else {
                throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : AllDocuments");
            }
            Writer.WriteEndElement();
        }
        
        public override void ReadXmlAttributes(global::Xtee.Core.IXteeXmlReader Reader) {
            base.ReadXmlAttributes(Reader);
        }
        
        public override void ReadXml(global::Xtee.Core.IXteeXmlReader Reader) {
            base.ReadXml(Reader);
            if (Reader.LocalName=="AllDocuments") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>>>GetDocumentListResponse>AllDocuments");
                }
                clazz.AllDocuments = new System.Collections.Generic.List<global::Doc.Xroad.Eu.Document>();
                if ((Reader.IsEmptyElement == false)) {
                    Reader.ReadStartElement("AllDocuments");
                    for (; ((Reader.NodeType != System.Xml.XmlNodeType.EndElement) 
                                && (Reader.LocalName == "item")); ) {
                        if ((Reader.IsElementNil() == false)) {
                            global::Xtee.Core.SerializationMetadata metaAllDocuments = this.GetMetaData<global::Doc.Xroad.Eu.Document>(Reader, "Document", "http://doc.x-road.eu/");
                            global::Doc.Xroad.Eu.Document item = metaAllDocuments.NewEntity<global::Doc.Xroad.Eu.Document>();
                            clazz.AllDocuments.Add(item);
                            if ((Reader.IsEmptyElement == false)) {
                                var allDocumentsSerializer = metaAllDocuments.NewSerializer(item);
                                allDocumentsSerializer.ReadXmlAttributes(Reader);
                                Reader.ReadStartElement("item");
                                allDocumentsSerializer.ReadXml(Reader);
                                Reader.ReadEndElement();
                            }
                            else {
                                Reader.Skip();
                            }
                        }
                        else {
                            clazz.AllDocuments.Add(null);
                            Reader.Skip();
                        }
                    }
                    Reader.ReadEndElement();
                }
                else {
                    Reader.Skip();
                }
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>>>GetDocumentListResponse>AllDocuments", Reader.LocalName);
            }
        }
    }
}
